<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<div class="portlet box green">
	<div class="portlet-title">
		<div class="caption">
			<i class="fa fa-gift"></i>
			<c:if test="${decidedzone != null }">定区编辑</c:if>
			<c:if test="${decidedzone == null }">定区添加</c:if>
		</div>
  </div>
  <div class="portlet-body form">
  	<form id="decidedzoneForm" class="form-horizontal form-bordered" action="/user/save" method="post" enctype="multipart/form-data">
  		
  		<div class="form-group">
        <label class="col-md-3 control-label">定区名称:</label>
        <div class="col-md-4">
        	<div class="input-icon right">
	        	<i class="fa"></i>
	          		<input type="text" class="form-control" name="name" value="${decidedzone.name}" required maxlength="50"/>
	        </div>
        </div>
      </div>
  		<div class="form-group">
        <label class="col-md-3 control-label">选择负责人:</label>
        <div class="col-md-4">
        	<div class="input-icon right">
	        	<i class="fa"></i>
	          	<select name="staffId" class="form-control select2" style="padding: 0;" required>
		          		
		        </select>
	        </div>
        </div>
      </div>
      
  		<div class="form-group">
        <label class="col-md-3 control-label">关联分区:</label>
        <div class="col-md-4">
        	<div class="input-icon right">
	        	<i class="fa"></i>
	          <select class="form-control select2" style="padding: 0;" id="subareaId" multiple>
	        		<c:forEach items="${subareas }" var="subarea">
	        			<option value="${subarea.id}" <c:if test="${subarea.decededzoneHas}">selected</c:if>>${subarea.addresskey}</option>
	        		</c:forEach>
	         </select>
	         <input type="hidden" name="subareaIds" id="subareaIds"/>
	        </div>
        </div>
      </div>
      
       <div class="form-actions">
        <div class="col-md-9 col-md-offset-3">
          <button type="button" class="btn btn-primary" onclick="savedecidedzone(this)">
          	<spring:message code="btn.save"></spring:message>
          </button>
          <button type="reset" class="btn btn-default" onclick="cancel()">
          	<spring:message code="btn.return"></spring:message>
          </button>
        </div>
      </div>
      <input type="hidden" name="id" value="${decidedzone.id}">
  	</form>
  </div>
</div>
<script type="text/javascript">

	$(function(){
		
		$("#subareaId").select2({
			allowClear: true
		});
		
		var staffListUrl = "${webRoot}/staff/listAjax";
		$.post(staffListUrl,{},function(result){
			if(result.code == 0) {
				//如果是编辑状态,取得staffId的值回显
				var staffId = "${decidedzone.staffId}";
				$.each(result.data,function(index, item){
					//如果staffId不为空，回显
					if(staffId != "") {
						if(staffId == item.id) 
							$("select[name='staffId']").append("<option value='"+ item.id +"' selected>"+ item.name +"</option>");
						else
							$("select[name='staffId']").append("<option value='"+ item.id +"'>"+ item.name +"</option>");
					}else{
						$("select[name='staffId']").append("<option value='"+ item.id +"'>"+ item.name +"</option>");
					}
				})
			}else{
				layer.msg(result.message, {shift: 6});
			}
		})
	})
	
	function savedecidedzone(obj){
		if($('#decidedzoneForm').valid()){
			
			var subareaIds = '';
			var selected = $('#subareaId option:selected');
			if(selected.length > 0) {
				$('#subareaId option:selected').each(function(i){
					subareaIds += $(this).val() + ',';
				});
				$('#subareaIds').val(subareaIds);
			}
			
			var formData = new FormData($('#decidedzoneForm')[0]);
			$.ajax({
				type: 'post',
				url: '<c:url value="/decidedzone/save"></c:url>',
				data: formData,
				async: false,  
			    cache: false,  
			    contentType: false,  
			    processData: false, 
				dataType: 'json',
				success: function(data){
					if(data.code == 0) {
						layer.msg(data.message, {shift: 1});
						setTimeout(function(){
							search();
							$(".panel-search").show();
							
						}, 1200);
						
					}else{
						layer.msg(data.message, {shift: 6});
					}
				}
			});
		}
	}
</script>